import time

from common.Message import *
from common.Tools import Tools
from page.main_page import MainPage
from page.general import General


class StagePage(General, metaclass=Tools):
    SELECT_CHAPTER = 'select_chapter.jpg'
    STAGE_HARD = 'stage_hard.jpg'
    SEVEN_SHADOWS = 'seven_Shadows.jpg'
    SKIP_STAGE = 'skip_stage.jpg'
    STAGE = 'stage.jpg'
    STAGE_CLEAR = 'stage_clear.jpg'
    ACTIVITY = 'activity.jpg'
    ACTIVITY_STAGE = 'activity_stage.jpg'
    ACTIVITY_STAGE_CLEAR = 'activity_stage_clear.jpg'
    GOLD_COIN = 'gold_coin.jpg'
    GOLD_COIN_CLEAR = 'gold_coin_clear.jpg'
    WORD_TREE = 'word_tree.jpg'

    # 点击关卡
    def click_stage(self):
        return self.click_match_xy(self.STAGE)

    # 点击活动
    def click_activity(self):
        return self.click_match_xy(self.ACTIVITY)

    # 点击七影列传
    def click_seven_shadows(self):
        return self.click_match_xy(self.SEVEN_SHADOWS)

    # 点击物资调度
    def click_gold_coin(self):
        return self.click_match_xy(self.GOLD_COIN)

    # 点击世界树
    def click_word_tree(self):
        return self.click_match_xy(self.WORD_TREE)

    # 点击困难
    def click_stage_hard(self):
        return self.click_match_xy(self.STAGE_HARD)

    # 点击选择章节
    def click_select_chapter(self):
        return self.click_match_xy(self.SELECT_CHAPTER)

    def list_down(self):
        print(SWIPING)
        return self.flick(self.windows_x / 4 * 3, self.windows_y / 5 * 4, self.windows_x / 4 * 3,
                          self.windows_y / 5 * 3)

    def list_up(self):
        print(SWIPING)
        return self.flick(self.windows_x / 4 * 3, self.windows_y / 5 * 3, self.windows_x / 4 * 3,
                          self.windows_y / 5 * 4)

    # 列表滑动查找
    def list_swipe_query(self, template_img, stop_template_img, template_threshold=0.8, stop_threshold=0.8):
        # 若未点击到匹配目标，则向下滑动列表
        while not self.get_match_xy(template_img, threshold=template_threshold):
            self.list_down()
            # 若向下滑动至停止目标，则往上滑动列表查找匹配目标
            if self.get_match_xy(stop_template_img,
                                 threshold=stop_threshold) and stop_template_img != template_img:
                while not self.get_match_xy(template_img, threshold=template_threshold):
                    self.list_up()
                return

    # 列表滑动点击
    def list_swipe_click(self, template_img, stop_template_img,template_threshold = 0.8,stop_threshold = 0.8):
        # 若未点击到匹配目标，则向下滑动列表
        self.list_swipe_query(template_img, stop_template_img, template_threshold, stop_threshold)
        return self.click_match_xy(template_img)

    # 选择关卡
    def click_chapter_stage(self, stage: str):
        return self.click_match_xy(f'chapter_stage_{stage}.jpg',threshold = 0.9)

    def skip_stage(self):
        return self.click_match_xy(self.SKIP_STAGE)

    def stage_process(self, stage, stop_stage, is_clear, is_clears=True):
        self.list_swipe_query(f'chapter_stage_{stage}.jpg', f'chapter_stage_{stop_stage}.jpg',template_threshold = 0.9, stop_threshold = 0.8)
        # 若关卡未全部完成，继续挑战
        while not self.is_stage_clear(stage, is_clear, is_clears):
            self.click_chapter_stage(stage)
            self.skip_stage()
            # 若无体力了，补充体力
            if self.get_match_xy(MainPage.MAGULO):
                self.food_eat()
            else:
                while not self.click_ok():
                    time.sleep(1)
            while not self.click_cancel():
                self.level_up_confirm()
                self.click_ok()

    # 判断关卡是否已无挑战次数
    def is_stage_clear(self, stage, is_clear, is_clears=True):

        clear_xy = self.get_match_xy(is_clear)
        stage_xy = self.get_match_xy(f'chapter_stage_{stage}.jpg',threshold=0.9)
        challenge_times_xy = self.get_match_xy('challenge_times.jpg')
        # 若挑战次数被遮挡，需要下滑列表
        if challenge_times_xy and challenge_times_xy[-1][1] < stage_xy[-1][1]:
            self.list_down()
            stage_xy = self.get_match_xy(f'chapter_stage_{stage}.jpg', threshold=0.9)
            clear_xy = self.get_match_xy(is_clear)

        print(is_clear,stage_xy)
        # 若存在多个已无挑战次数关卡，需要根据纵坐标判断已无次数的关卡是否为当前需要挑战的关卡，是则退出挑战，否则继续挑战
        if is_clears and clear_xy and clear_xy[-1][1] > stage_xy[-1][1]:
            print(STAGE_NO_TIMES)
            return True
        # 若仅有一个已无挑战次数关卡，则退出挑战，否则继续挑战
        elif not is_clears and clear_xy:
            print(STAGE_NO_TIMES)
            return True
        else:
            print(STAGE_CHALLENGING)
            return False

    # 全关卡流程
    def all_stage_process(self, entry_func, chapters: list[str], stages: list[str] or list[tuple[str]],
                          stop_chapter=None, stop_stage='4', is_clear='stage_clear.jpg',
                          is_clears=True):
        """

        :param entry_func:到关卡前的步骤，因每个关卡的入口不同所以自己定义方法
        :param stop_chapter: 章节列表停止目标，表示若章节未找到时，自动滑动至改目标后往回找
        :param chapters:关卡前的入口列表
        :param stages:关卡列表
        :param stop_stage:同chapter_stop_tamp
        :param is_clear: 关卡列表已无挑战次数标识，当识别到该标识时跳过关卡
        :param is_clears:关卡列表是否可能存在多个无挑战次数的标识图片，若无则传False
        :return: None

        """
        for i, chapter in enumerate(chapters):
            self.click_stage()
            # 关卡入口方法
            if stop_chapter:
                entry_func(chapter, stop_chapter)
            else:
                entry_func(chapter)
            self.click_stage_hard()
            if len(stages) == 1:
                for stage in stages[0]:
                    self.stage_process(stage, stop_stage, is_clear, is_clears)
            else:
                for stage in stages[i]:
                    self.stage_process(stage, stop_stage, is_clear, is_clears)

    # 选择七影列传章节入口
    def click_sevens_shadows_chapter_entry(self, chapter: str, stop_chapter):
        self.click_seven_shadows()
        self.click_select_chapter()
        self.click_match_xy(f'seven_shadows_{chapter[0]}.jpg')
        return self.list_swipe_click(f'seven_shadows_{chapter}.jpg',
                                     f'seven_shadows_{stop_chapter}.jpg')

    # 盗贼讨伐入口
    def click_gold_coin_entry(self, chapter_name):
        self.click_gold_coin()
        return self.click_match_xy(f'{chapter_name}.jpg')

    # 点击活动入口
    def click_activity_entry(self, activity_name):
        self.click_activity()
        self.list_swipe_click(f'{activity_name}.jpg',
                              f'halloween_night.jpg')
        while not self.click_match_xy(self.ACTIVITY_STAGE):
            time.sleep(2)
        return True
